package zee.com.customviewexample;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.zee.utils.UIUtils;
import com.zee.utils.ZListUtils;

import java.util.ArrayList;
import java.util.List;

public class MyView extends View {
    private float width;//控件宽度
    private float height;//空间高度
    private float rect_w;//柱状图的宽度
    private List<Animator> mAnimators = new ArrayList<>();
    private RectF r1, r2, r3;
    Paint paint1 = new Paint();

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void init() {
        r1 = new RectF((float) (width * 0.1), scaleYFloats[0], (float) (width * 0.1 + rect_w), height);
        r2 = new RectF((float) (width * 0.1 + rect_w * 2), scaleYFloats[1], (float) (width * 0.1 + rect_w * 3), (height));
        r3 = new RectF((float) (width * 0.1 + rect_w * 4), scaleYFloats[2], (float) (width * 0.1 + rect_w * 5), (height));
        createAnimation();
        Color.argb(255, 255, 92, 92);
        paint1.setColor(UIUtils.getColor(R.color.color_custom));
        paint1.setStyle(Paint.Style.FILL);
        paint1.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = View.MeasureSpec.getSize(heightMeasureSpec);
        width = View.MeasureSpec.getSize(widthMeasureSpec);
        rect_w = (float) (width * 0.9 / 5);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画柱状；动态图，就要改变柱状的top值
        r1.top = scaleYFloats[0];
        r2.top = scaleYFloats[1];
        r3.top = scaleYFloats[2];

        canvas.drawRect(r1, paint1);
        canvas.drawRect(r2, paint1);
        canvas.drawRect(r3, paint1);
    }

    public static final float SCALE = 1.0f;

    float[] scaleYFloats = new float[]{SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,};

    public void createAnimation() {
        long[] delays = new long[]{100, 300, 150};
        long[] dutions = new long[]{1400, 1000, 1800};
        for (int i = 0; i < 3; i++) {
            final int index = i;
            ValueAnimator scaleAnim = ValueAnimator.ofFloat(1, height - 1, 1);
            scaleAnim.setDuration(dutions[i]);
            scaleAnim.setRepeatCount(-1);
            scaleAnim.setStartDelay(delays[i]);
            scaleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    scaleYFloats[index] = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            scaleAnim.start();
            mAnimators.add(scaleAnim);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (ZListUtils.isEmpty(mAnimators)) {
            return;
        }
        int count = mAnimators.size();
        for (int i = 0; i < count; i++) {
            Animator animator = mAnimators.get(i);
            animator.cancel();
            animator.removeAllListeners();
        }
        mAnimators.clear();
        mAnimators = null;
    }
}